class Admin::ArtworksController < Admin  
  # GET /artworks
  # GET /artworks.xml
  def index
    @artworks = Artwork.find(:all)
    @page_title = Artwork.l_attr(:list)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @artworks }
    end
  end

  # GET /artworks/1
  # GET /artworks/1.xml
  def show
    @artwork = Artwork.find(params[:id])
    @page_title = l('common', :viewing) + " " + Artwork.name + " " + @artwork.name
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @artwork }
    end
  end

  # GET /artworks/new
  # GET /artworks/new.xml
  def new
    @artwork = Artwork.new
    @page_title = Artwork.l_attr('new')
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @artwork }
    end
  end

  # GET /artworks/1/edit
  def edit
    @artwork = Artwork.find(params[:id])
    @page_title = "Editing Artwork: " + @artwork.name + " by " + @artwork.artist.name
  end

  # POST /artworks
  # POST /artworks.xml
  def create
    @artwork = Artwork.new(params[:artwork])
    @artwork.added_by = current_user
    
    respond_to do |format|
      if @artwork.save
        flash[:notice] = Artwork.l_attr('created')
        format.html { redirect_to edit_admin_artwork_url(@artwork) }
        format.xml  { render :xml => @artwork, :status => :created, :location => @artwork }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @artwork.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /artworks/1
  # PUT /artworks/1.xml
  def update
    @artwork = Artwork.find(params[:id])
    @artwork.last_edited_by = current_user

    respond_to do |format|
      if @artwork.update_attributes(params[:artwork])
        flash[:notice] = 'Artwork was successfully updated.'
        format.html { redirect_to :back }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @artwork.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /artworks/1
  # DELETE /artworks/1.xml
  def destroy
    @artwork = Artwork.find(params[:id])
    @artwork.destroy

    respond_to do |format|
      format.html { redirect_to(admin_artworks_url) }
      format.xml  { head :ok }
    end
  end
  
  def publish
    @artwork = Artwork.find(params[:id])
    @page_title = l('common', :publishing) + " " + Artwork.l_name + " " + @artwork.name
    
    respond_to do |format|
      format.html
      format.xml  { render :xml => @artwork }
    end
  end

  def add_category
    @artwork = Artwork.find(params[:artwork])
    @artwork.last_edited_by = current_user
    
    if !@artwork.categories.include? Category.find(params[:category])
      @artwork.categories << Category.find(params[:category])
      @artwork.save
    end 
    
    render :partial => "admin/categories/list", :layout => false
  end
  
  def remove_category
    @artwork = Artwork.find(params[:id])
    @artwork.last_edited_by = current_user
    
    @artwork.categories.delete Category.find(params[:cat])
    @artwork.save
    
    render :partial => "admin/categories/list", :layout => false
  end
  
  include AdminHelper  
  def change_published
    @artwork = Artwork.find(params[:artwork])
    @artwork.last_edited_by = current_user
    
    if params[:act] == 'publish'
      check_requirements :artwork
      if meets_requirements?
        @artwork.published = true
        @artwork.save
      end
    elsif params[:act] == 'unpublish'
        @artwork.published = false
        @artwork.save      
    end
    
    respond_to do |format|
      format.html { redirect_to :back }
      format.xml  { render :xml => @artwork }
    end    
  end
end
